﻿package com.wly.ui
{
	import com.wly.data.ItemVO;
	import com.wly.data.ResourceCache;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class ItemSketchy extends Sprite
	{
		private var _tfName:TextField;
		private var _mcImg:Sprite;
		private var _bmp:Bitmap;
		private var _mcBg:MovieClip;
		
		private var _item:ItemVO;

		public var itemGrid:ItemGrid;
		public var index:int;
		
		static public const ITEMHEIGHT:int = 50;
		
		public function ItemSketchy()
		{
			super();
			
			_tfName = getChildByName("tfName") as TextField;
			_mcImg  = getChildByName("mcImg") as Sprite;
			_mcBg   = getChildByName("mcBg") as MovieClip;
			_mcBg.stop();
			
			addEventListener(MouseEvent.CLICK, onSelect);
		}
		
		public function updateItemInfo(item:ItemVO):void
		{
			_item = item;
			if(!_item)
			{
				return;
			}
			
			_tfName.text = _item.url;
			
			if(_bmp)
			{
				_mcImg.removeChild(_bmp);
				_bmp = null;
			}
			
			_bmp = new Bitmap;
			_bmp.bitmapData = ResourceCache.getInstance().getResourseByUrl(_item.url) as BitmapData;
			_mcImg.addChild(_bmp);
		}
		private function onSelect(e:MouseEvent):void
		{
			if(itemGrid)
			{
				itemGrid.setSelect(index);
			}
		}
		
		public function set selected(val:Boolean):void
		{
			if(val)
			{
				_mcBg.gotoAndStop(2);
			}
			else
			{
				_mcBg.gotoAndStop(1);
			}
		}
	}
}